

class ImageThing < Thing
  #attr_accessor :image_id
  attribute :image_id
  attribute :desired_width
  attribute :desired_height
  attribute :float


  DEFAULT_SIZE = ['400', '600']

  def desired_width
    @desired_width || DEFAULT_SIZE[0]
  end

  def desired_height
    @desired_height || DEFAULT_SIZE[1]
  end

  def recieve e

    if e.params[form_id(:desired_width)]
      self.desired_width = e.params[form_id(:desired_width)]
      self.desired_height = e.params[form_id(:desired_height)]
      self.float = e.params[form_id(:float)]
    end

    if e.params[form_id(:file)]

      #p e.params[form_id(:file)]

      @tried ||= 0
      @tried += 1

      #puts "tryyyyyyyyyyyyyyyyyyyyyyyyying "+form_id(:file)+" t"+@tried.to_s

      file = ImageFileRecord.new({:file_name => e.params[form_id(:file)][:filename]})

      #p e.params[id][:tempfile].read

      file.data = e.params[form_id(:file)][:tempfile].read
      file.save || (raise 'shiiit')

      self.image_id = file.id
    end

    super
  end

  def medium_size
    @medium_size || 500
  end


  def creator e
    #<input type="file" name="img"/>

    do_float = Element.new(:input, @content.to_s, {
        :type => :checkbox,
        :name => form_id(:float),
        :id => form_id(:float),
      })

    if float
      do_float.checked = 'checked'
    end

    img = image_el 64, 128
    img.set :style, "float:left; margin-right:0.5em;"

    img.to_s +
    Element.new(:label, 'šířka: ', :for => form_id(:desired_width)).to_s+
    Element.new(:input, @content.to_s,
      { :type => :text, :name => form_id(:desired_width), :value => desired_width, :size => 4}
    ).to_s + ', ' +
    Element.new(:label, 'výška: ', :for => form_id(:desired_height)).to_s+
    Element.new(:input, @content.to_s,
      { :type => :text, :name => form_id(:desired_height), :value => desired_height, :size => 4}
    ).to_s + '<br>' +

    do_float.to_s +
    Element.new(:label, ' obtékat', :for => form_id(:float)).to_s+
    '<br>' +

    Element.new(:input, @content.to_s,
      { :type => :file, :name => form_id(:file) }
    ).to_s
  end

  def render e
    #@content

    if e.data['page']
      e.page.head << <<FANCYBOX
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js" type="text/javascript"></script>
        <script type="text/javascript" src="/js/fancybox/jquery.easing-1.3.pack.js"></script>
        <script type="text/javascript" src="/js/fancybox/jquery.mousewheel-3.0.2.pack.js"></script>
        <script type="text/javascript" src="/js/fancybox/jquery.fancybox-1.3.0.js"></script>
        <link rel="stylesheet" type="text/css" href="/js/fancybox/jquery.fancybox-1.3.0.css" media="screen" />
FANCYBOX
    end

    if image_id
      img = image_el
      if float
        element.style = "float:left;"
        element.class_name += ' '+self.class.to_s+"-leftFloat"
      end
      Element.new(:a,
        img.to_s,
        :href => "/images/image/#{image_id.to_s}.jpg",
        :id => form_id
      ).to_s + <<JAVASCRIPT
<script>
(function() {
  var fancybox_is_dragging = false;
  $('##{form_id}').fancybox({
      showCloseButton : false,
      overlayShow : false,
      hideOnContentClick : false,
      titleShow : false,
      transitionIn : 'none',
      transitionOut	: 'none',
      onComplete : function(a, b, c, inner) {
        inner.click(function(e) {
          if (!fancybox_is_dragging) {
            $.fancybox.close();
          }
          fancybox_is_dragging = false;
        }).parent().draggable({
          start : function(e) {
            fancybox_is_dragging = true;
          }
        });
      }
    });
})();
</script>
JAVASCRIPT

    else
      ''
    end
  end

  def image_el w = desired_width, h = desired_height
    Element.new(:img,
        :src => "/images/image/#{image_id.to_s}.jpg?width=#{w}&height=#{h}",
        :width => w
      )
  end

end
